package 用队列实现栈;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> q1 = new LinkedList<>();
    Queue<Integer> q2 = new LinkedList<>();
    int count = 0;
    public MyStack() {

    }

    public void push(int x) {
        if(q1.isEmpty()){
            q2.add(x);
        }else {
            q1.add(x);
        }
        count++;
    }

    public int pop() {
        int ret = 0;
        int num = count;
        if (q1.isEmpty()){
            while (count!=1){
                ret = q2.poll();
                q1.add(ret);
                count--;
            }
            ret = q2.poll();
        }else{
            while (count!=1){
                ret = q1.poll();
                q2.add(ret);
                count--;
            }
            ret = q1.poll();
        }
        count = num;
        count--;
        return ret;
    }

    public int top() {
        int ret = 0;
        if (q1.isEmpty()){
            while (!q2.isEmpty()){
                ret = q2.poll();
                q1.add(ret);
            }
        }else{
            while (!q1.isEmpty()){
                ret = q1.poll();
                q2.add(ret);
            }
        }
        return ret;
    }

    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();
    }
}